﻿(function () {
    var self,
        ajaxSendMessage = {
            errorTemplate: "<div  class='alert alert-block alert-error messageconfir'><a class='close' data-dismiss='alert' href='#'><i class='icon-remove'></i></a>{{message}}</div> ",
            confirmTemplate: '<div  class="alert alert-block alert-success messageconfir"><a class="close" data-dismiss="alert" href="#"><i class="icon-remove"></i></a>{{message}}</div> ',
            pleaseWaitDiv: $('<div class="modal hide" id="pleaseWaitDialog" data-backdrop="static" data-keyboard="false"><div class="modal-header"><h1>Processing...</h1></div><div class="modal-body"><div class="progress progress-striped active"><div class="bar" style="width: 100%;"></div></div></div></div>'),
            init: function () {
                self = this;
                this.bindEvents();
            },
            bindEvents: function () {
                $("#Send").on('click', self.submitRequest);
            },
            submitRequest: function (e) {
                e.preventDefault();
                $("#myModal").modal('show');
                $("#btnYes").on("click", function () {
                    var url = $("#urlsend").data('url');
                    $.ajax({
                        url: url,
                        dataType: "json",
                        beforeSend: function () {
                            $("#myModal").modal("hide");
                            ajaxSendMessage.pleaseWaitDiv.modal();
                        },
                        success: function () {
                            ajaxSendMessage.pleaseWaitDiv.modal("hide");
                        }
                    })
                        .done(function (response) {
                            self.setMessages(response.message, response.success);
                        });
                });

                $("#btnNo").on("click", function () {
                    $("#myModal").modal("hide");
                });
            },

            setMessages: function (message, confirm) {
                var alert = "";
                if (confirm) {
                    alert = self.confirmTemplate.replace("{{message}}", message);
                    $("#Send").hide();
                } else
                    alert = self.errorTemplate.replace("{{message}}", message);

                $("#FormAjaxSubmitNotifications").show();
                $('#FormAjaxSubmitNotifications').html(alert);
                $('html,body').animate({ scrollTop: $("#FormAjaxSubmitNotifications").offset().top }, 500);

                setTimeout(function () { $('#FormAjaxSubmitNotifications').fadeOut('fast'); }, 3000);
            }
        };
    ajaxSendMessage.init();
})(jQuery);